const uniIdCo = uniCloud.importObject("uni-id-co", {
		errorOptions: {
			type: 'toast'
		}
	});
const uniObj_user = uniCloud.importObject("user-app-obj");
const state = {
	token:'',
	token_expired:'',
	userInfo: {},
	AllUserInfo:[],
	userTotal:0,
	newToken: {},
	hasLogin: false
};
const mutations = {
	SETUSERINFO(state,result){
		state.userInfo = result.data;
		
		state.hasLogin = true;
	},
	GETUSERINFO(state, userInfo) {
		state.userInfo = userInfo;
		// state.newToken = {}
	},
	LOGOUT(state) {
		// 清除本地存储
		uni.clearStorageSync();
		state.userInfo = {};
		state.token = '';
		state.token_expired = '';
		state.hasLogin = false;
	},
	
	CHOOSEAVATAR(state, avatarUrl) {
		// state.userInfo.avatarUrl = avatarUrl;
		state.userInfo = Object.assign(state.userInfo, {
			avatarUrl: avatarUrl
		});
		// this.$set(state.userInfo,'avatarUrl',avatarUrl)
	},
	GETUSERALL(state,result){
		state.AllUserInfo = result.data;
		state.userTotal = result.count;
	}
};
const actions = {
	// 存储用户信息
	async setUserInfo({
		commit
	}, uid) {
		const result = await uniObj_user.getUserInfo(uid)
		if(result.errCode==0){
			commit("SETUSERINFO", result);
			
		}
		
		
	},
	async getUserInfoForToken({commit},token){
		const result = await uniObj_user.getUserInfoForToken(token);
		if(result.errCode==0){
			commit("SETUSERINFO", result);
		}
	},
	
	// 选择头像
	chooseAvatar({
		commit
	}, avatarUrl) {
		commit("CHOOSEAVATAR", avatarUrl);
	},
	
	// 退出登录
	async logOut({
		commit
	}) {
		const out = await uniIdCo.logout();
		commit("LOGOUT")
	},
	async getUserAll({commit}){
		const result = await uniObj_user.getUserAll();
		if(result.code==0){
			commit('GETUSERALL',result)
		}
	}
	
};
const getters = {};

export default {
	state,
	mutations,
	actions,
	getters
}